package com.fengkai.filepost.pdfoutstream.test.ODsolutions;


import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fengkai  敏感字符串加密
 * @creat 2023/4/2
 */
public class StringEntry {

    //1
    //password__a12345678_timeout_100
    public static void main (String[] args) {

        System.out.println(getEncry(2, "password__\"a12345678\"_\"timeout_100\""));
    }

    public static String getEncry (int index, String str) {

        //将双引号内的下划线置换
        Pattern compile = Pattern.compile("\"(.*?)\"+");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer("");
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group().replace("_", "!"));
            System.out.println(matcher.group());
        }
        String s = matcher.appendTail(sb).toString();
        System.out.println(s);
        int left = 0;
        int right = 0;
        int indexCache = 0;
        ArrayList<String> commondList = new ArrayList<>();
        ArrayList<String> keywordList = new ArrayList<>();
        String[] s1 = s.split("_");
        for (int i = 0; i < s1.length; i++) {
            if (1 == 1) {
                if (!"".equals(s1[i])){
                    commondList.add(s1[i]);
                }
            }
        }
        if (commondList.size() < index) {
            return "ERROR";
        }
        commondList.set(index, commondList.get(index).contains("\"") ? "\"******\"" : "******");
        String out = "";
        for (String s2 : commondList) {
            out += s2 + "_";
        }
        String substring = out.substring(0, out.length() - 1);
        Pattern compile2 = Pattern.compile("\"(.*?)\"+");
        Matcher matcher1 = compile2.matcher(substring);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher1.find()) {
            matcher1.appendReplacement(stringBuffer, matcher1.group().replaceAll("!", "_"));
        }
        return matcher1.appendTail(stringBuffer).toString();
    }
}
